package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.StudentService;
import com.itheima.edu.info.manager.service.TeacherService;


import java.util.Scanner;

public class TeacherController {

    public void start() {
        Scanner sc=new Scanner(System.in);
        co:while(true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加老师");
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除老师");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改老师");
                    updataStudent();
                    break;
                case "4":
                    //System.out.println("查看所有老师");
                    quarryTeacher();
                    break;
                case "5":
                    System.out.println("退出");
                    break co;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void updataStudent() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您要修改的老师学号");
        while(true){
            String sid=sc.next();
            boolean exits=TeacherService.isExits(sid);
            if(!exits){
                System.out.println("您输入的学号不存在，请重新输入");
            }else{
                System.out.println("请输入新的老师姓名");
                String name = sc.next();
                System.out.println("请输入新的老师年龄");
                String age = sc.next();
                System.out.println("请输入新的老师生日");
                String birthday = sc.next();
                Teacher tea=new Teacher();
                tea.setId(sid);
                tea.setName(name);
                tea.setAge(age);
                tea.setBirthday(birthday);
                TeacherService.updataTeacher(tea,sid);
                System.out.println("修改成功");
                break;
            }
        }
    }

    public void deleteTeacher() {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您要删除的老师学号");
        while(true){
            String sid = sc.next();
            boolean exits = TeacherService.isExits(sid);
            if(!exits){
                System.out.println("您输入的学号不存在，请重新输入");
            }else{
                TeacherService.deleteTeahcer(sid);
                System.out.println("删除成功");
                break;
            }
        }


    }

    public void quarryTeacher() {
        Teacher[] tea=TeacherService.quarryTeacher();
        if(tea==null){
            System.out.println("暂时无数据,请先进行添加");
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < tea.length; i++) {
            if(tea[i]!=null){
                System.out.println(tea[i].getId() +"\t"+tea[i].getName()+ "\t" +tea[i].getAge()+ "\t\t" +tea[i].getBirthday());
            }
        }
    }


    public void addTeacher() {
        Scanner sc=new Scanner(System.in);
         String sid;
        while(true){
            System.out.println("请输入老师学号");
            sid = sc.next();
           boolean flag = TeacherService.isExits(sid);
           if(flag){
               System.out.println("您输入的学号已经存在，请重新输入");
           }else{
               break;
           }
        }
            System.out.println("请输入老师姓名");
            String name = sc.next();
            System.out.println("请输入老师年龄");
            String age = sc.next();
            System.out.println("请输入老师生日");
            String birthday = sc.next();
            Teacher tea = new Teacher();
            tea.setId(sid);
            tea.setName(name);
            tea.setAge(age);
            tea.setBirthday(birthday);
            boolean result= TeacherService.addteacher(tea);
            if(result){
                System.out.println("添加成功");

            }else{
                System.out.println("添加失败");
            }
    }
}
